package com.example.carbonbackend.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.carbonbackend.common.BaseResponse;
import com.example.carbonbackend.common.ErrorCode;
import com.example.carbonbackend.common.ResultUtils;
import com.example.carbonbackend.exception.BusinessException;
import com.example.carbonbackend.exception.ThrowUtils;
import com.example.carbonbackend.model.dto.massage.MassageAddRequest;
import com.example.carbonbackend.model.dto.massage.MassageQueryRequest;
import com.example.carbonbackend.model.entity.Massage;
import com.example.carbonbackend.model.entity.User;
import com.example.carbonbackend.model.vo.MassageVO;
import com.example.carbonbackend.service.MassageService;
import com.example.carbonbackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息表(Massage)表控制层
 *
 * @author makejava
 * @since 2024-03-27 21:43:25
 */
@RestController
@RequestMapping("massage")
@Slf4j
public class MassageController  {


    @Resource
    UserService userService;

    @Resource
    MassageService massageService;

    /**
     * 创建
     *
     * @param massageAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addMassage(@RequestBody MassageAddRequest massageAddRequest, HttpServletRequest request) {
        // 判断是否添加记录
        if (massageAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Massage massage = new Massage();
        BeanUtils.copyProperties(massageAddRequest, massage);

        boolean save = massageService.save(massage);

        ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);

        return ResultUtils.success(massage.getSenderId());

    }


    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<MassageVO> getMassageVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Massage massage = massageService.getById(id);
        if (massage == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(massageService.getMassageVO(massage, request));
    }

    /**
     * 分页获取列表（仅管理员）
     *
     * @param massageQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<Massage>> listMassageByPage(@RequestBody MassageQueryRequest massageQueryRequest) {
        long current = massageQueryRequest.getCurrent();
        long size = massageQueryRequest.getPageSize();
        Page<Massage> massagePage = massageService.page(new Page<>(current, size),
                massageService.getQueryWrapper(massageQueryRequest));
        return ResultUtils.success(massagePage);
    }

    /**
     * 分页获取列表（封装类）
     *
     * @param massageQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<MassageVO>> listMassageVOByPage(@RequestBody MassageQueryRequest massageQueryRequest,
                                                         HttpServletRequest request) {
        long current = massageQueryRequest.getCurrent();
        long size = massageQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Massage> massagePage = massageService.page(new Page<>(current, size),
                massageService.getQueryWrapper(massageQueryRequest));
        return ResultUtils.success(massageService.getMassageVOPage(massagePage, request));
    }

    /**
     * 分页获取当前用户创建的资源列表
     *
     * @param massageQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<MassageVO>> listMyMassageVOByPage(@RequestBody MassageQueryRequest massageQueryRequest,
                                                           HttpServletRequest request) {
        log.info("查询信息{}", massageQueryRequest);
        if (massageQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        massageQueryRequest.setReceiverId(loginUser.getId());
        long current = massageQueryRequest.getCurrent();
        long size = massageQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Massage> massagePage = massageService.page(new Page<>(current, size),
                massageService.getQueryWrapper(massageQueryRequest));

        return ResultUtils.success(massageService.getMassageVOPage(massagePage, request));
    }

    @GetMapping("/getUnReadNum")
    public BaseResponse<Long> getUnReadNum(HttpServletRequest request)
    {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        MassageQueryRequest massageQueryRequest = new MassageQueryRequest();
        massageQueryRequest.setIsRead(0);
        massageQueryRequest.setReceiverId(loginUser.getId());

        long count = massageService.count(massageService.getQueryWrapper(massageQueryRequest));

        return ResultUtils.success(count);
    }

    @GetMapping("/getUnRead")
    public BaseResponse<List<MassageVO>> getUnRead(HttpServletRequest request)
    {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        MassageQueryRequest massageQueryRequest = new MassageQueryRequest();
        massageQueryRequest.setIsRead(0);
        massageQueryRequest.setReceiverId(loginUser.getId());
        List<MassageVO> massageVOS = new ArrayList<>();
        List<Massage> list = massageService.list(massageService.getQueryWrapper(massageQueryRequest));
        for (Massage massage : list) {
            MassageVO massageVO = new MassageVO();
            BeanUtils.copyProperties(massage,massageVO);
            massageVOS.add(massageVO);
        }
        return ResultUtils.success(massageVOS);
    }
}

